Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
Adding an alert that says "Not found!" if the input box on search bar is nothing

I am trying to add an alert that says "not found" when you search for nothing using a search bar that I have already programmed in JavaScript. Something like this:

if (document.getElementById('query').value = *variable for nothing*) {
alert('Not found!')
}

This is example code. Can someone give me a line of code that would do this but is functional?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is one of the few cases where the solution is to write no additional code at all!

You can examine the truthiness of the .value of your element to see if it has a value in it. That means you would check the condition:

if(document.getElementById('query').value) {

If that's truthy, then it has a value. If it's falsey then it does not have a value.

document.getElementById('test').addEventListener('click', () => {
  if (document.getElementById('query').value) {
    alert('It has a value');
  } else {
    alert('It does not have a value');
  }
});
<input id="query" type="text" />
<button id="test">Test</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda